//
//  WQNetworkTools.swift
//  WeiBo
//
//  Created by 张德华 on 7/23/16.
//  Copyright © 2016 ZhangDeHua. All rights reserved.
//

import UIKit
import AFNetworking


// MARK: - ************请求方式 枚举
enum WQRequestMethod: String {
    case GET = "GET"
    case POST = "POST"
}

class WQNetworkTools: AFHTTPSessionManager {
    
    
    // MARK: - ************创建全局访问点(单例)
    static let sharedManager: WQNetworkTools = {
        
        let tools = WQNetworkTools(baseURL: nil)
        
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return tools
    }()
    
    
    
    
    ///  封装网络请求
    ///
    ///  - parameter method:     method 请求方法  GET&POST
    ///  - parameter URLString:  URLString 请求地址
    ///  - parameter parameters: parameters 参数(字典)
    ///  - parameter finished:   finished 完成回调(JSON字典)
    func request(method: WQRequestMethod, URLString: String, parameters: [String: AnyObject]?, finished: (result: [String: AnyObject]?) -> ()) {
        
        ///------ 成功回调
        let success = {(task: NSURLSessionDataTask, responseObject: AnyObject?) in
            
            if let result = responseObject as? [String: AnyObject] {
                finished(result: result)
            } else {
                print("数据请求错误")
                finished(result: nil)
            }
        }
        
        ///------ 失败回调
        let failure = {(task: NSURLSessionDataTask?, error: NSError) in
            print("网络请求错误")
            finished(result: nil)
        }
        
        
        
        ///------ 判断请求方式,做出相应请求
        if method == .GET {
            ///------ GET请求
            self.GET(URLString, parameters: parameters, progress: nil, success: success, failure: failure)
        } else {
            ///------ POST请求
            self.POST(URLString, parameters: parameters, progress: nil, success: success, failure: failure)
        }
        
    }
}

// MARK: - ************OAuth界面数据请求
extension WQNetworkTools {
    
    ///------ 获取授权
    func oauthLoadUserAccount(code: String, finished: (result: [String: AnyObject]?) -> ()) {
        
        let urlString = "https://api.weibo.com/oauth2/access_token"
        
        let params = ["client_id": appKey,
                      "client_secret": appSecret,
                      "grant_type": "authorization_code",
                      "code": code,
                      "redirect_uri": redirectUri]
   
        request(.POST, URLString: urlString, parameters: params, finished: finished)
    }
    
    
    ///------ 请求用户信息数据
    func oauthLoadUserInfo(userAccount: WQUserAccountModel, finished: (result: [String: AnyObject]?) -> ()) {
        
        let urlString = "https://api.weibo.com/2/users/show.json"
        
        let params = ["access_token": userAccount.access_token!, "uid": userAccount.uid!]
    
        request(.GET, URLString: urlString, parameters: params, finished: finished)
    }
    
    
    
    
    
}

// MARK: - 请求首页微博界面数据
extension WQNetworkTools{
    func homeLoadData(sinceId: Int64, maxId: Int64, finished: (result: [String: AnyObject]?) -> ()){
        // 请求urlString
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        // 请求参数
        let params = [
            "access_token": WQUserAccountViewModel.sharedTools.accessToken!,
            "since_id":"\(sinceId)",
            "max_id":"\(maxId)"
        ]
        request(.GET, URLString: urlString, parameters: params, finished: finished)
        
    }
}




